package com.yeung.controller;

import com.yeung.beans.Department;
import com.yeung.beans.Employee;
import com.yeung.dao.DepartmentDao;
import com.yeung.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;

    @PostMapping("/login")
    public String login(@RequestParam("userName") String userName,
                        @RequestParam("psw") String psw, ModelMap map, HttpSession session) {
        if (!StringUtils.isEmpty(userName) && psw.equals("1")) {
            //为了防止重复提交,可以重定向到需要跳转的网页, 而不是直接请求
            session.setAttribute("loginUser", userName);
            return "redirect:/main.html";
        } else {
            //登陆失败

            map.put("errMsg", "用户名或者密码错误");
            return "index";
        }
    }

    /**
     * 解决更新操作时,诸如数据与数据库数据不一致的问题
     *
     * @param id
     * @param map
     */
    @ModelAttribute
    public void getEmployee(@RequestParam(value = "id", required = false) Integer id, ModelMap map) {
        if (id != null)
            map.put("employee", employeeDao.getEmployeeById(id));
    }

    @GetMapping(value = "/emp")
    public String toAddPage(Map<String, Object> map) {
        map.put("departments", departmentDao.getDepartments());
        return "emp/add";
    }

    @PutMapping(value = "/emp")
    public String update(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/user/emps";
    }

    @GetMapping(value = "/emp/{id}")
    public String toEditPage(@PathVariable(value = "id") Integer id, ModelMap map) {
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", employeeDao.getEmployeeById(id));
        return "emp/add";
    }

    @DeleteMapping(value = "/emp/{id}")
    public String delete(@PathVariable(value = "id", required = false) Integer id) {
        employeeDao.delete(id);
        return "redirect:/user/emps";
    }

    @PostMapping(value = "/emp")
    public String save(Employee employee) {
        System.out.println("going to save employee: " + employee);
        Department department = departmentDao.getDepartmentById(employee.getDepartment().getId());
        employee.setDepartment(department);
        employeeDao.save(employee);
        return "redirect:/user/emps";//重定向到list页面s
    }

    @GetMapping("/emps")
    public String employeeList(Map<String, Object> map) {
        Collection<Employee> employess = employeeDao.getEmployess();
        map.put("emps", new ArrayList<>(employess));
        //thymeleaf 默认就会拼串
        //class path:templates/xxx.html
        return "emp/list";
    }
}
